﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FlotasDesktop;
using System.Data.SqlClient;


namespace GestorDeFlotasDesktop.AbmAuto
{
    public partial class ListadoAuto : Form
    {
        String conexion;
        BajaAuto formAbrirBaja;
        ModificacionAuto formModificacionAuto;

        public ListadoAuto()
        {
            InitializeComponent();
            Utils utils = new Utils();
            conexion = utils.cargarProperties();
            this.rellenarComboBoxMarcas();
            this.rellenarComboBoxSerieReloj();
            dataGridView1.Enabled = false;
        }

        public void setearFormBaja(BajaAuto baja)
        {
            this.formAbrirBaja= baja;
            this.formModificacionAuto = null;
        }

        public void setearFormModificacion(ModificacionAuto modificacion)
        {
            this.formModificacionAuto = modificacion;
            this.formAbrirBaja = null;
        }
        
      
        private void buscarButton_Click(object sender, EventArgs e)
        {
            using (SqlConnection connection = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command = new SqlCommand())
                    {
                        dataGridView1.Enabled = true;
                        connection.Open();
                        command.Connection = connection;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "TEQUILASO.sp_buscar_automovil";

                        command.Parameters.Add(new SqlParameter("@MarcaAuto", marcaComboBox.Text));
                        command.Parameters.Add(new SqlParameter("@Licencia", licenciaTextBox.Text));
                        command.Parameters.Add(new SqlParameter("@Modelo", modeloTextBox.Text));
                        command.Parameters.Add(new SqlParameter("@Patente", patenteTextBox.Text));
                        command.Parameters.Add(new SqlParameter("@Reloj_nro_serie", modeloRelojComboBox.Text));
                        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                        {
                            DataSet ds = new DataSet();
                            adapter.Fill(ds);
                            dataGridView1.DataSource = ds.Tables[0];

                        }


                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        public void rellenarComboBoxSerieReloj()
        {
            using (SqlConnection connection = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command = new SqlCommand())
                    {
                        command.Connection = connection;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "TEQUILASO.sp_seleccionar_reloj";
                        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                        {
                            DataTable dt = new DataTable();
                            adapter.Fill(dt);
                            modeloRelojComboBox.DataSource = dt;
                            modeloRelojComboBox.DisplayMember = "Nro_serie";

                        }
                        modeloRelojComboBox.SelectedItem = null;

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error al cargar modelos Reloj");
                }
            }
        }
        public void rellenarComboBoxMarcas()
        {
            using (SqlConnection connection = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command = new SqlCommand())
                    {
                        command.Connection = connection;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "TEQUILASO.sp_marca_automovil";
                        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                        {
                            DataTable dt = new DataTable();
                            adapter.Fill(dt);
                            marcaComboBox.DataSource = dt;
                            marcaComboBox.DisplayMember = "descripcion";

                        }
                        marcaComboBox.SelectedItem = null;

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error al cargar marcas de automovil");
                }
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                this.ejecutarSeleccion(e);
            }
           
        }

        private void ejecutarSeleccion(DataGridViewCellEventArgs e)
        {
            String idAuto = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            String licencia = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            String rodado = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            String id_marca = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            String modelo = dataGridView1.CurrentRow.Cells[5].Value.ToString();
            String patente = dataGridView1.CurrentRow.Cells[6].Value.ToString();
            String id_reloj = dataGridView1.CurrentRow.Cells[7].Value.ToString();
            Boolean habilitado = (Boolean)dataGridView1.CurrentRow.Cells[8].Value;
            if (this.formAbrirBaja != null && this.formModificacionAuto == null)
            {
                BajaAuto baja = new BajaAuto();
                baja.mostrateConCampos(idAuto, licencia, rodado, id_marca, modelo, patente, id_reloj, habilitado);
            }
            else
            {
                ModificacionAuto modificacion = new ModificacionAuto();
                modificacion.mostrateConCampos(idAuto, licencia, rodado, id_marca, modelo, patente, id_reloj, habilitado);

            }
        }

        private void limpiarButton_Click(object sender, EventArgs e)
        {
            marcaComboBox.Text = "";
            licenciaTextBox.Text = "";
            modeloRelojComboBox.Text = "";
            patenteTextBox.Text = "";
            modeloTextBox.Text = "";
        }


   
    }
}
